<?php

namespace app\admin\model;

use think\Model;

class ExaminationQuestion extends Model
{
    protected $autoWriteTimestamp = true;

    public static function getList($pid)
    {
        $where = [];
        $where[] = ['pid', '=', $pid];
        $limit = input('get.limit');
        if (input('get.title')) $where[] = ['b.title', 'like', '%' . input('get.title') . '%'];
        $list = self::alias('a')
            ->join('questions_list b', 'a.qid = b.id')
            ->order('a.id', 'desc')
            ->field('a.id,a.score,b.title,b.type,b.difficulty')
            ->where($where)
            ->paginate($limit);
        foreach ($list as $k => $v) {
            $list[$k]['type_title'] = QuestionsList::$typeList[$v['type']]['title'] ?? '类型已删';
            $list[$k]['type_class'] = QuestionsList::$typeList[$v['type']]['class'] ?? '';
        }
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public static function getCountAndTotalByPid($pid)
    {
        return self::where(['pid' => $pid])->field('count(*) as numbers,sum(score) as scores')->find();
    }

    public static function getAvgDifficultyByPid($pid)
    {
        return self::alias('a')
            ->join('questions_list b', 'a.qid = b.id')
            ->where(['a.pid' => $pid])
            ->avg('difficulty');
    }

    public static function getInfoById($id)
    {
        return self::alias('a')
            ->join('questions_list b', 'a.qid = b.id')
            ->field('a.id,a.score,a.qid,b.title')
            ->where('a.id', '=', $id)
            ->find();
    }

    public static function getInfoByQid($pid, $qid, $id)
    {
        return self::where('pid', '=', $pid)
            ->where('qid', '=', $qid)
            ->where('id', '<>', $id)
            ->find();
    }
}